package me.zhengjie.hr.positionbase.service.impl;

import me.zhengjie.hr.positionbase.domain.PositionBase;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.hr.positionbase.repository.PositionBaseRepository;
import me.zhengjie.hr.positionbase.service.PositionBaseService;
import me.zhengjie.hr.positionbase.service.dto.PositionBaseDto;
import me.zhengjie.hr.positionbase.service.dto.PositionBaseQueryCriteria;
import me.zhengjie.hr.positionbase.service.mapper.PositionBaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Service
//@CacheConfig(cacheNames = "positionBase")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PositionBaseServiceImpl implements PositionBaseService {

    private final PositionBaseRepository positionBaseRepository;

    private final PositionBaseMapper positionBaseMapper;

    public PositionBaseServiceImpl(PositionBaseRepository positionBaseRepository, PositionBaseMapper positionBaseMapper) {
        this.positionBaseRepository = positionBaseRepository;
        this.positionBaseMapper = positionBaseMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(PositionBaseQueryCriteria criteria, Pageable pageable){
        Page<PositionBase> page = positionBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(positionBaseMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<PositionBaseDto> queryAll(PositionBaseQueryCriteria criteria){
        return positionBaseMapper.toDto(positionBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public PositionBaseDto findById(Integer id) {
        PositionBase positionBase = positionBaseRepository.findById(id).orElseGet(PositionBase::new);
        ValidationUtil.isNull(positionBase.getId(),"PositionBase","id",id);
        return positionBaseMapper.toDto(positionBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public PositionBaseDto create(PositionBase resources) {
        return positionBaseMapper.toDto(positionBaseRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(PositionBase resources) {
        PositionBase positionBase = positionBaseRepository.findById(resources.getId()).orElseGet(PositionBase::new);
        ValidationUtil.isNull( positionBase.getId(),"PositionBase","id",resources.getId());
        positionBase.copy(resources);
        positionBaseRepository.save(positionBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            positionBaseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PositionBaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PositionBaseDto positionBase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("职位名称", positionBase.getPosition());
            map.put("职位描述", positionBase.getPositionInf());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}